//
//  UserZhuangBeiController.m
//  CoolTennisBall
//
//  Created by Coollang on 16/8/30.
//  Copyright © 2016年 CoolLang. All rights reserved.
//

#import "UserZhuangBeiController.h"
#import "UserEquipModel.h"
#import "UserShoesCell.h"
#import "UserRacketCell.h"
#import "UserEquipModel.h"


#define ksectionHeadHeight kAutoHei(30)
#define ksectionFootHeight kAutoHei(400)

@interface UserZhuangBeiController ()

@end

@implementation UserZhuangBeiController {
    UserEquipModel *_viewModel;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    _viewModel = [User getLastUser].userEquipModel;
    
    [self setTableviewStyle];
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
}


- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    [self getUserEquipInfro];
}

// 获取用户的装备信息
- (void)getUserEquipInfro {
     __weak typeof(self) weakSelf = self;
    YFUserApi *equipApi = [YFUserApi requestGetUserDataWithReqType:kUserGetEquipApi];
    
    [[YFNetworkingService sharedInstance] sendRequest:equipApi callback:^(BOOL success, id result, NSError *error) {
        if (success) {
            UserEquipModel *equipModel = [[UserEquipModel alloc] init];
            NSDictionary *dic = [equipApi responseInfo];
            [equipModel parseEquipDictionary:dic];
            User *user = [User getLastUser];
            RLMRealm *realm = [RLMRealm defaultRealm];
            [realm beginWriteTransaction];
            user.userEquipModel = equipModel;
            [realm commitWriteTransaction];
            _viewModel = [User getLastUser].userEquipModel;
            [weakSelf.tableView reloadData];
        }else {
            
        }
    }];
}

// 获取装备信息
- (void)setTableviewStyle {
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    
    self.tableView.showsVerticalScrollIndicator = NO;
    self.tableView.showsHorizontalScrollIndicator = NO;

    // 设置背景色
    self.tableView.backgroundColor = [UIColor colorWithHex:0xf3f3f3];
    // 去掉分割线
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
}


#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = nil;
    if (indexPath.section == 0) {
       cell = [tableView dequeueReusableCellWithIdentifier:@"UserRacketCell" forIndexPath:indexPath];
        UserRacketCell *racketCell = (UserRacketCell *)cell;
        racketCell.viewModle = _viewModel;
    }else {
        cell = [tableView dequeueReusableCellWithIdentifier:@"UserShoesCell" forIndexPath:indexPath];
        UserShoesCell *shoeCell = (UserShoesCell *)cell;
        shoeCell.viewModle = _viewModel;
    }
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {
        return 374;
    }else {
        return 148;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    return ksectionHeadHeight;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    
    return 0.01;
    
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    UIView *headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kYFScreenWidth, ksectionHeadHeight)];
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 0, kYFScreenWidth - 20, headView.bounds.size.height)];
    label.font = [UIFont systemFontOfSize:12];
    label.textColor = [UIColor colorWithHex:0x151515 alpha:0.5];
    [headView addSubview:label];
    headView.backgroundColor = [UIColor colorWithHex:0xf3f3f3];
    if (section == 0) {
        label.text = NSLocalizedString(@"Racket", nil);
    }else if (section == 1){
        label.text = NSLocalizedString(@"User.Shoes", nil);
    }
    return headView;
}

@end
